/*
 * File     : DebugAllocator.java
 * Created  : 26 Apr 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import com.googlecode.dni.Allocator;
import com.googlecode.dni.type.Pointer;


/**
 * Implementation that logs allocations.
 *
 * @author Matthew Wilson
 */
final class DebugAllocator implements Allocator
{

    private static final Logger LOGGER = Logger.getLogger( "DNI.Allocator" );

    private final Allocator allocator;

    private final Map< Pointer, Object > POINTERS = new ConcurrentHashMap< Pointer, Object >();

    /**
     * @param allocator
     *            the delegate allocator
     */
    DebugAllocator( final Allocator allocator )
    {
        this.allocator = allocator;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Pointer allocate( final int size )
    {
        LOGGER.info( "Allocating " + size + " bytes" );

        Pointer pointer = this.allocator.allocate( size );

        Object previous = this.POINTERS.put( pointer, Boolean.TRUE );
        if ( previous != null )
        {
            LOGGER.warning( "Reallocating same pointer; was memory freed via native code?" );
        }

        LOGGER.info( "Allocated " + size + " bytes at " + pointer );

        return pointer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Pointer copy( final Pointer sourcePointer, final int size )
    {
        LOGGER.info( "Copy-allocating " + size + " bytes" );

        Pointer pointer = this.allocator.copy( sourcePointer, size );
        Object previous = this.POINTERS.put( pointer, Boolean.TRUE );
        if ( previous != null )
        {
            LOGGER.warning( "Reallocating same pointer; was memory freed via native code?" );
        }

        LOGGER.info( "Copy-allocated " + size + " bytes at " + pointer );

        return pointer;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void free( final Pointer pointer )
    {
        LOGGER.info( "Freeing " + pointer );

        Object existing = this.POINTERS.remove( pointer );
        if ( existing == null )
        {
            throw new AssertionError( "Double-free found: " + pointer );
        }

        this.allocator.free( pointer );
    }

}
